import random
import time

from com.xunai.saas.example.Common import CommonRequest
from com.xunai.saas.example.TongueExample import TongueApi


class CorporeityApi(CommonRequest):
    """
    中医智能体质检测
    """

    def start(self, sex, age):
        """
        开始
        :param sex:
        :param age:
        :return:
        """
        param = {"sex": sex, "age": age}
        rs = super().post("/xunai-saas/saasApi/v1/corporeityDiagnose/start", param)
        print(rs.json())
        return rs.json()['data']['recordNo']

    def handle(self, recordNo, answer):
        """
        循环答题
        :param recordNo:
        :param answer:
        :return:
        """
        param = {"recordNo": recordNo, "answer": answer}
        rs = super().post("/xunai-saas/saasApi/v1/corporeityDiagnose/handle", param)
        print(rs.json())
        opt = rs.json()['data']
        if opt and opt['recordStatus'] != 3:
            # 随机选择一个答案
            answer = {"questionCode": opt['questionCode'],
                      "answerCode": opt['answerList'][random.randint(0, len(opt['answerList']) - 1)]['code']}
            self.handle(recordNo, answer)
        else:
            print("诊断结束")

    def result(self, recordNo):
        """
        获取结果
        :param recordNo:
        :return:
        """
        rs = super().get("/xunai-saas/saasApi/v1/corporeityDiagnose/result?recordNo=" + str(recordNo))
        print(rs.json())

    def tongue(self, recordNo, tongueNotifyCode):
        """
        结合舌诊
        :param recordNo:
        :param tongueNotifyCode:
        :return:
        """
        param = {"recordNo": recordNo, "tongueNotifyCode": tongueNotifyCode}
        rs = super().post("/xunai-saas/saasApi/v1/corporeityDiagnose/addTongueRecord", param)
        print(rs.json())


if __name__ == "__main__":
    # 体质辨证
    corporeityApi = CorporeityApi()
    recordNo = corporeityApi.start("male", 12)
    print(recordNo)
    corporeityApi.handle(recordNo, None)
    corporeityApi.result(recordNo)

    # 关联舌诊
    tongueApi = TongueApi()
    rs = tongueApi.analysis("舌上图像", "舌下图像", "通知地址")
    time.sleep(10)
    corporeityApi.tongue(recordNo, rs['data']['notifyCode'])
    corporeityApi.result(recordNo)
